package gomoku;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Root extends javax.swing.JFrame implements MouseListener{
	private BufferedImage background;
	private GameLogic gameLogic = new GameLogic();
	private int stoneWidth = 50;
	private int stoneHeight = 50;
	private JButton jButton1;
	private AbstractAction abstractAction1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Root inst = new Root();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public Root() {
		super();
		initGUI();
		background = tryLoadingImg("grid15large.png");
		
	}
	
	private BufferedImage tryLoadingImg(String filepath){
		BufferedImage img = null;
		try {
			img = ImageIO.read(new File(filepath));
		} catch (Exception e) {
			System.out.println("Couldnt find file");
		}
		return img;
	}
	
	public void paint(Graphics g){
		//g.setColor(Color.white);
		//g.fillRect(0, 0, 750, 750);
		g.drawImage(background,-1,0,null);
		Graphics2D g2 = (Graphics2D) g;
		//1,1 = 45,64
		
		//Code to draw a filled circle representing
		//the player who is in turn to make a play.
		//Only slightly helpful, initial test proved it not
		//very useful.
		/*if(gameLogic.playerOnesTurn){
			g2.setPaint(Color.black);
		}
		else{						
			g2.setPaint(Color.lightGray);	
		}
		g2.fill(new Ellipse2D.Double(230, 800, 40, 40));
		*/
		for(int i=0; i<15;i++){
			for(int j=0; j<15;j++){
				int k = gameLogic.getPosInfo(i, j);
				if(k!=0){
					double x_pixel = (-5) + 50.5*i;
					double y_pixel = 14 + 49.5*j;
					if(k==1){
						g2.setPaint(Color.black);
					}
					else{						
						g2.setPaint(Color.lightGray);	
					}
					g2.fill(new Ellipse2D.Double((int) x_pixel, (int) y_pixel, stoneWidth, stoneHeight));
				}
			}
		}	
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			{
				jButton1 = new JButton();
				jButton1.setText("Restart");
				jButton1.setAction(getAbstractAction1());
			}
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 578, Short.MAX_VALUE));
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(777, Short.MAX_VALUE)
				.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap());
			this.addMouseListener(this);
			pack();
			this.setSize(750, 815);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		
		//Compensate for the drawImage arguments,
		//but leave some for a "click buffer"...
		//x -= 10;
		y -= 20;
		
		x = x/50;
		y = y/50;
		if(x<15 && y<15)gameLogic.update(x, y);
		
		//System.out.println(gameLogic.getPosInfo(x, y));
		repaint();
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	private AbstractAction getAbstractAction1() {
		if(abstractAction1 == null) {
			abstractAction1 = new AbstractAction("Restart", null) {
				public void actionPerformed(ActionEvent evt) {
					gameLogic.restartGame();
					System.out.println("Game restarted!");
					repaint();
					
				}
			};
		}
		return abstractAction1;
	}

}
